/**
 * Created by songj on 2015/12/11.
 */
define(['angular',
    'framework/msg/GillionMsgModule',
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/datasource/DataSourceModule',
    'framework/dropdown/GillionDropdownModule',
    'framework/date/DateModule',
    'angular-underscore'
], function (angular) {
    var module = angular.module('PublishController',
        ['GillionMsgModule', 'DataGridModule', 'GillionPaginationModule',
            'DataSourceModule', 'DateModule', 'GillionDropdownModule'])
        .controller('PublishController', ['$scope', 'GillionMsgService', '$dataSourceManager', '$timeout', 'Arrays', "$location",'Resource','GillionMsg','$compile','$http','$compile',
                function ($scope, GillionMsgService, $dataSourceManager, $timeout, Arrays, $location,Resource,GillionMsg,$compile) {
                    $scope.mes = GillionMsgService.getInputData();
                    if($scope.mes.packageStatus==0){
                        $scope.packageStatus="启用";
                    }else if($scope.mes.packageStatus==1){
                        $scope.packageStatus="停用";
                    }else if($scope.mes.packageStatus==2){
                        $scope.packageStatus="终止";
                    }
                    $scope.nowVersion=true;
                    $scope.historyVersion=false;
                    $scope.search = {
                        groupId:  $scope.mes.groupId,
                        artifactId: $scope.mes.artifactId
                    };
                    $scope.showVersion= function(type){
                        if(type==1){
                            $scope.nowVersion=true;
                            $scope.historyVersion=false;
                        }else{
                            $scope.nowVersion=false;
                            $scope.historyVersion=true;
                        }
                    }

                    $scope.confirm = function() {
                        if ($dataSourceManager.dataSources["unitCaseSource"].records.length == 0) {
                            GillionMsg.alert('提示', '请编辑测试用例！');
                            return;
                        }

                        GillionMsgService.setOutputData(Arrays.extractToArray($dataSourceManager.dataSources["unitCaseSource"].records, 'id'));
                        GillionMsgService.close();
                    };

                    $scope.hisPublish= function(row){
                        if (row.status == 1) {
                            GillionMsg.alert('提示', '该版本已经是发布！');
                            return;
                        }
                        Resource("/grule-manager/rulePackage/publish/:id", {
                            id: "@id"
                        }, {
                            "get": {
                                "failHandler": false
                            }
                        }).update({
                            id: row.id
                        }, function (response) {
                            if (response.success) {
                                GillionMsg.alert('提示消息', '切换为发布成功！', function(){
                                    GillionMsgService.close();
                                });
                            }
                        });
                    }

                }
            ]
        );

    module.filter('statusFilter', function () {
        return function (input) {
            var valueTypeDict = ['编辑中', '已发布', '已封版'];
            return valueTypeDict[input] || '';
        };
    });
    module.config(['$locationProvider', function($locationProvider) {
        $locationProvider.html5Mode(true);
    }]);
    return module;
});
